package suanfa.datastructure.tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

//9.如何实例化一个二叉树，并实现对二叉树的遍历。
 class Tree1 {
    node1 tree;
    public Tree1(node1 root){
        this.tree=root;
    }
    public void front(){
        if(this.tree!=null){
            this.tree.preorder();
        }else{
            System.out.println("根节点为空");
        }

    }


    //10.假如我们希望在二叉树上查找一个数据，返回有还是没有，这个查询方法怎么写？
    public node1 find(String data,node1 n){
        boolean t=false;
        if(n.name.equals(data)){
            System.out.println("已发现");
            return n;
        }
        if(n.left!=null){
            find(data,n.left);
        }
        if(n.right!=null){
            find(data,n.right);
        }
        return n;

    }


    public static void main(String[] args) {
        node1 root=new node1("root");
        node1 n1=new node1("1");
        node1 n2=new node1("2");
        node1 n3=new node1("3");
        Tree1 t=new Tree1(root);
        root.setleft(n1);
        root.setright(n2);
        n1.setleft(n3);
        t.front();
        t.find("3",root);

    }


}
 class node1 {
    String name;
    node1 left, right;

    public node1(String name) {
        this.name = name;
        this.left = null;
        this.right = null;
    }

    public void setleft(node1 left) {
        this.left = left;
    }

    public void setright(node1 right) {
        this.right = right;
    }

     public void preorder(){
         System.out.println(this.name);
         if(this.left!=null){
             this.left.preorder();
         }
         if(this.right!=null){
             this.right.preorder();
         }
     }
}
